package com.tutor.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tutor.common.Result;
import com.tutor.entity.User;
import com.tutor.mapper.UserMapper;
import com.tutor.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Page pageCC(Page<User> page, Wrapper wrapper) {
        return userMapper.pageCC(page,wrapper);
    }

    @Override
    public Result updatePassword(User user) {
        //1.进行一些非空判断
        if(user.getName() == null || "".equals(user.getName())){
            return Result.fail("Error: empty username");
        }
        if(user.getPassword() == null || "".equals(user.getPassword())){
            return Result.fail("Error: empty password");
        }
        String phone = userMapper.getPhoneByName(user);
//        System.out.println("phone:"+phone);


        if((!user.getPhone().equals(phone)) ){
            return Result.fail("Error: invalid phone number");
        }
        try{
            userMapper.updatePassword(user);
        }catch (Exception e){
            return Result.fail("Error sys err");
        }

        Result r = new Result();
        r.setCode(200);
        r.setMsg("Success");
        return r;
    }

    @Override
    public Result register(User user) {
        Integer userExist = userMapper.selectByNo(user);
        System.out.println(userExist);
        if( userExist > 0){
            return Result.fail("User already registered");
        }
        //System.out.println(no);
        userMapper.insert(user);
        Result r = new Result();
        r.setCode(200);
        r.setMsg("Success");
        return r;

    }


}
